package com.example.vaultmysql.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;

/**
 * 测试执行实体类
 * 
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "test_execution")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TestExecution {

    @Id
    @Column(length = 50)
    private String id;

    @Column(name = "test_name", length = 200, nullable = false)
    private String testName;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", length = 20, nullable = false)
    private TestStatus status = TestStatus.PENDING;

    @Column(name = "concurrent_users", nullable = false)
    private Integer concurrentUsers;

    @Column(name = "duration_seconds", nullable = false)
    private Integer durationSeconds;

    @Enumerated(EnumType.STRING)
    @Column(name = "query_type", length = 20, nullable = false)
    private QueryType queryType;

    @Column(name = "start_time")
    private LocalDateTime startTime;

    @Column(name = "end_time")
    private LocalDateTime endTime;

    @Column(name = "created_by", length = 100)
    private String createdBy;

    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 测试状态枚举
     */
    public enum TestStatus {
        PENDING("待执行"),
        RUNNING("执行中"),
        COMPLETED("已完成"),
        FAILED("执行失败"),
        CANCELLED("已取消");

        private final String description;

        TestStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 查询类型枚举
     */
    public enum QueryType {
        SELECT("查询操作"),
        INSERT("插入操作"),
        UPDATE("更新操作"),
        DELETE("删除操作"),
        MIXED("混合操作");

        private final String description;

        QueryType(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 构造函数
     */
    public TestExecution(String id, String testName, Integer concurrentUsers, 
                        Integer durationSeconds, QueryType queryType, String createdBy) {
        this.id = id;
        this.testName = testName;
        this.concurrentUsers = concurrentUsers;
        this.durationSeconds = durationSeconds;
        this.queryType = queryType;
        this.createdBy = createdBy;
        this.status = TestStatus.PENDING;
    }

    /**
     * 开始测试
     */
    public void start() {
        this.status = TestStatus.RUNNING;
        this.startTime = LocalDateTime.now();
    }

    /**
     * 完成测试
     */
    public void complete() {
        this.status = TestStatus.COMPLETED;
        this.endTime = LocalDateTime.now();
    }

    /**
     * 测试失败
     */
    public void fail() {
        this.status = TestStatus.FAILED;
        this.endTime = LocalDateTime.now();
    }

    /**
     * 取消测试
     */
    public void cancel() {
        this.status = TestStatus.CANCELLED;
        this.endTime = LocalDateTime.now();
    }

    /**
     * 获取执行时长（秒）
     */
    public Long getExecutionDuration() {
        if (startTime == null || endTime == null) {
            return null;
        }
        return java.time.Duration.between(startTime, endTime).getSeconds();
    }

    /**
     * 是否正在运行
     */
    public boolean isRunning() {
        return TestStatus.RUNNING.equals(status);
    }

    /**
     * 是否已完成
     */
    public boolean isCompleted() {
        return TestStatus.COMPLETED.equals(status) || 
               TestStatus.FAILED.equals(status) || 
               TestStatus.CANCELLED.equals(status);
    }
}